import axios from "axios";

const state = {
    adminName: localStorage.adminName,
    admintoken: localStorage.admintoken,
    adminLoginList: [],
    adminList: [],
    poetryList: []
}

const mutations = {
    //获取管理员登录日志列表
    CHANGE_ADMIN_LOGIN_LIST(state, adminLoginList) {
        state.adminLoginList = adminLoginList;
    },
    //获取管理员列表
    CHANGE_ADMIN_LIST(state, adminList) {
        state.adminList = adminList;
    },
    //获取古诗词列表
    CHANGE_POETRY_LIST(state, poetryList) {
        state.poetryList = poetryList;
    }
}

const actions = {
    //添加管理员
    addAdmin(state, adminForm) {
        return axios.post("/addAdmin", adminForm);
    },
    //获取管理员登录日志列表
    getadminLoglist({ commit }, params) {
        axios.get('/getadminLoglist', { params }).then(res => {
            commit('CHANGE_ADMIN_LOGIN_LIST', res.getList);
            commit('CHANGE_PAGE_INDEX', res.pageIndex);
        })
    },
    //获取管理员列表
    getAdminList({ commit }) {
        axios.get('/getAdminList').then(res => {
            commit('CHANGE_ADMIN_LIST', res.result);
        })
    },
    //删除管理员
    deleteAdmin(state, _id) {
        return axios.delete('/deleteAdmin', {
            params: {
                _id
            }
        })
    },
    //添加古诗词
    addPoetry(state, poetryForm) {
        return axios.post("/addPoetry", poetryForm)
    },
    //查询古诗词列表
    getPoetryList({ commit }, params) {
        axios.get("/getPoetryList", { params }).then(res => {
            commit('CHANGE_POETRY_LIST', res.getList);
            commit('CHANGE_PAGE_INDEX', res.pageIndex);
        })
    },
    //根据诗词年代查询诗词
    getPoetryListByDynasty({ commit }, params) {
        axios.get("/getPoetryListByDynasty", { params }).then(res => {
            commit('CHANGE_POETRY_LIST', res.getList);
            commit('CHANGE_PAGE_INDEX', res.pageIndex);
        })
    },
    //根据诗人查询诗词
    getPoetryListByPoetryPote({ commit }, params) {
        axios.get("/getPoetryListByPoetryPote", { params }).then(res => {
            commit('CHANGE_POETRY_LIST', res.getList);
            commit('CHANGE_PAGE_INDEX', res.pageIndex);
        })
    },

    //删除古诗词
    deletePoetry(state, _id) {
        return axios.delete('/deletePoetry', {
            params: {
                _id
            }
        });
    },
    //修改古诗词信息
    putPoetry(state, poetryForm) {
        return axios.put("/putPoetry", poetryForm);
    }
}

export default {
    state, mutations, actions
}